<?php

class UsersController extends AppController
{
    var $name = 'Users';
	var $helpers = array('Html', 'Javascript', 'Ajax');

	function index()
    {		/*$this->checkSession();
        $this->set('users', $this->User->findAll());*/
        $this->set('grid', $this->requestAction('/grids/getGridContent'),array('return'));
    }
    
    function getGridContent()
    {
		$this->set('users', $this->requestAction('/grids/getGridContent'),array('return'));
	}

	function listmember()
    {
		$this->checkSession();
        $this->set('users', $this->User->findAll());
    }

	function view($id = null)
    {
		$this->checkSession();
        $this->User->id = $id;
        $this->set('user', $this->User->read());
    }

	function getUser($id = null)
    {
		$this->checkSession();
        return $this->User->find("id=$id");
    }

	function register()
    {
        if (!empty($this->data))
        {
			$someone = $this->User->findByNick($this->data['User']['nick']);
			if(empty($someone))
			{
				//Justin add
				$this->data['User']['password'] = ($this->data['User']['password']);
				if ($this->User->save($this->data))
				{
					$this->flash('User has been saved.','/users');
				}
			}
			else
			{
				$this->flash('This user account has existed.','/users/register');
			}
        }
    }

	function edit($id = null)
	{
		$this->checkSession();
		if (empty($this->data))
		{
			$this->User->id = $id;
			$this->data = $this->User->read();
		}
		else
		{
			if ($this->User->save($this->data['User']))
			{
				$this->flash('This user has been updated.','/users');
			}
		}
	}

	function delete($id)
	{
		$this->checkSession();
		$this->User->del($id);
		$this->flash('The user with id: '.$id.' has been deleted.', '/users');
	}

	function login()
    {
        $this->set('error', false); 

        if (!empty($this->data))
        {
            $someone = $this->User->findByNick($this->data['User']['nick']);

			//Justin add md5
            if(!empty($someone['User']['password']) && $someone['User']['password'] == $this->data['User']['password'])
            {

                $this->Session->write('User', $someone['User']);

                $this->redirect('/users/home');
            }
            else
            {
                $this->set('error', true);
            }
        }
    }

	function logout()
    {

        $this->Session->delete('User');
     
        $this->redirect('/users/login');
    }
    
    function jiangjiangshowusers()
    {
    	$this->set('knownusers', 
         $this->User->findAll(null, array('id', 'UserName', 'Gender'), 'id DESC')
       );

    }

	function home()
	{
		$this->checkSession();

		$userId = $this->Session->read('User.id');

		if ($userId)
		{
			$this->set('myPlan', $this->requestAction("Plans/getLatestPlan/$userId")); 

			$allStudentId = $this->requestAction("Mentors/getStudentId/$userId");
			if ($allStudentId)
			{
				$studentId = $allStudentId['Mentor']['user_id'];
				$this->set('student', $this->requestAction("Users/getUser/$studentId")); 
				$this->set('studentPlan', $this->requestAction("Plans/getLatestPlan/$studentId")); 
			}

			$this->set('allQuestion', $this->requestAction("Questions/getNotDealt")); 
		}
	}

}

?>
